/**
 * Auto gen via dbpojo-gen.
 */
package com.us.usws.site.console.action;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import org.apache.commons.lang3.StringUtils;

import com.us.usws.common.SiteConstants;
import com.us.usws.model.domain.video.Video;
import com.us.usws.model.domain.video.VideoFile;
import com.us.usws.model.flat.system.DOUser;
import com.us.usws.service.cud.VideoCUDService;
import com.us.usws.service.ro.VideoROService;
import com.us.usws.support.uitls.FilterDto;
import com.us.usws.support.uitls.FilterModel;
import com.us.usws.support.uitls.JSONDataUtil;
import com.us.usws.support.uitls.Order;
import com.us.usws.support.uitls.SiteWebContext;
import com.us.usws.support.uitls.WebContext;

/**
 * Struts2 action
 * @author robot
 */
public class VideoAction extends BasePageSearchAction<Video> {

	private static final long serialVersionUID = 1L;

	private Video video;
	
	private VideoFile videoFile;

	@Inject
	private VideoROService videoROService;

	@Inject
	private VideoCUDService videoCUDService;

	private String uids;
	
	private String channelUid;
	
	/**
	 * ajax load the list of Video
	 */
	public void ajaxLoadVideos() {
		createDataPageUtil();
		dataPageUtil.setOrder(Order.desc("create_date"));
		if(video != null) {
			Map<FilterDto, Object> filter = new HashMap<FilterDto, Object>(); 
			
			String name = video.getName();
			Integer typeSid = video.getTypeSid();
			if(StringUtils.isNotBlank(name)) {
				filter.put(new FilterDto("name", FilterModel.LIKE), name);
			}
			if(typeSid != null) {
				filter.put(new FilterDto("type_sid", FilterModel.EQ), typeSid);
			}
			if(filter.size() > 0) {
				dataPageUtil.setFilter(filter);
			}
		}
		
		videoROService.loadVideosByPage(dataPageUtil);
		getOut().println(JSONDataUtil.extJsonSupportPage(dataPageUtil.getResult(), dataPageUtil.getRowCount()));
	}
	
	/**
	 * ajax load can add video for the channel
	 */
	public void ajaxLoadVideosAbleAdd() {
		createDataPageUtil();
		videoROService.loadVideosAbleAddByPage(dataPageUtil, channelUid);
		getOut().println(JSONDataUtil.extJsonSupportPage(dataPageUtil.getResult(), dataPageUtil.getRowCount()));

	}
	
	/**
	 * load channel's video
	 */
	public void ajaxLoadChannelVideos() {
		createDataPageUtil();
		videoROService.loadChannelVideosByPage(dataPageUtil, channelUid);
		getOut().println(JSONDataUtil.extJsonSupportPage(dataPageUtil.getResult(), dataPageUtil.getRowCount()));
	}

	/**
	 * ajax update Video
	 */
	public void ajaxUpdateVideo() {
		getResponse().setContentType("text/html;charset=UTF-8");
		if (video != null) {
			String path = getRealPath();
			Date now = new Date(System.currentTimeMillis());
			WebContext wc = new SiteWebContext(getRequest(), getResponse());
			DOUser user = wc.getUser();
			
			String playLen = video.getPlayLen();
			if(StringUtils.isNotBlank(playLen)) {
				String[] lens = StringUtils.split(playLen, ":");
				videoFile.setPlayLen((Integer.parseInt(lens[0])*60*60+Integer.parseInt(lens[1])*60 + Integer.parseInt(lens[2]))*1000L);
			}
			
			String uid = video.getUid();
			if (StringUtils.isNotBlank(uid)) {
				video.setLastUpdByUid(user.getUid());
				video.setLastUpdDate(now);
				video.setAuthStatusDsid(SiteConstants.VIDEO_AUTH_STATUS_PENDING);
				video.setSeqNum(0);
				videoFile.setLastUpdByUid(user.getUid());
				videoFile.setLastUpdDate(now);
				videoFile.setUid(video.getFileUid());
				
				String snapshot = videoFile.getSnapshot();
				if(StringUtils.isNotBlank(snapshot) && snapshot.contains("/")) {
					videoFile.setSnapshot(path + snapshot);
				}
				
				String videoPath = videoFile.getVideoPath();
				if(StringUtils.isNotBlank(videoPath) && videoPath.contains("/")) {
					videoFile.setVideoPath(path + videoPath);
				}
				
				videoCUDService.updateVideo(video, videoFile);
			} else {
				video.setCreateByUid(user.getUid());
				video.setCreateDate(now);
				video.setSeqNum(0);
				video.setAuthStatusDsid(SiteConstants.VIDEO_AUTH_STATUS_PENDING);
				
				videoFile.setCreateByUid(user.getUid());
				videoFile.setCreateDate(now);
				String videoPath = videoFile.getVideoPath();
				String snapshot = videoFile.getSnapshot();
				if(StringUtils.isNotBlank(snapshot)) {
					videoFile.setSnapshot(path + snapshot);
				}
				
				if(StringUtils.isNotBlank(videoPath)) {
					videoFile.setVideoPath(path + videoPath);
				}
				videoCUDService.addVideo(video, videoFile);
			}
			getOut().print(true);
		} else {
			getOut().print(false);
		}
	}

	/**
	 * ajax delete user.
	 */
	public void ajaxChangeVideoStatus() {
		WebContext wc = new SiteWebContext(getRequest(), getResponse());
		DOUser user = wc.getUser();
		videoCUDService.changeVideoStatus(uids, user.getUid(), video.getAuthStatusDsid());
	}
	
	public String getChannelUid() {
		return channelUid;
	}

	public void setChannelUid(String channelUid) {
		this.channelUid = channelUid;
	}

	public void setVideo(Video video) {
		this.video = video;
	}

	public Video getVideo() {
		return video;
	}


	public String getUids() {
		return uids;
	}

	public void setUids(String uids) {
		this.uids = uids;
	}

	public VideoFile getVideoFile() {
		return videoFile;
	}

	public void setVideoFile(VideoFile videoFile) {
		this.videoFile = videoFile;
	}

}